<?php

namespace app\admin\controller\agent;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;
use function request;

/**
 * Undocumented class
 * @title 代理等级
 */
class Agentlevel extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        $this->dataModel = new model\AgentLevel();
        $this->pageTitle = '推客等级';
        $this->pagesize = 10;
        $this->sortOrder = 'level asc';

        $this->selectTextField = '{name}';
        $this->selectFields = 'id,name';
        $this->selectSearch = 'name|level';
    }

    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];

        if (!empty($searchData['name'])) {
            $where[] = ['name', 'like', '%' . $searchData['name'] . '%'];
        }

        return $where;
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('name', '名称', 3)->maxlength(20);
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('name', '名称')->getWrapper()->addStyle('width:150px');
        $table->show('level', '等级');
        $table->show('member_count', '人数统计');
        $table->show('score', '上级积分');
        $table->show('description', '描述');
        $table->show('create_time', '添加时间')->getWrapper()->addStyle('width:150px');
        $table->show('update_time', '更新时间')->getWrapper()->addStyle('width:150px');

        $table->useCheckbox(false);
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
        $form->tab('基本信息')->with(
            function () use ($form, $isEdit) {
                $form->fields('', '', 10)->size(0, 12)->showLabel(false)->with(
                    function () use ($form, $isEdit) {
                        $form->defaultDisplayerSize(12, 12);
                        $form->text('name', '名称');
                        $form->number('level', '等级');
                        $form->textarea('description', '描述');
                        $form->number('score', '上级积分')->help('下级买水架升级/邀请下级，上级获得积分');
                        if ($isEdit) {
                            $form->show('member_count', '人数统计');
                            $form->show('create_time', '添加时间');
                            $form->show('update_time', '更新时间');
                        }
                    }
                );
            }
        );

        $form->tab('方式1/桶数爬坡升级')->with(
            function () use ($form, $isEdit) {
                $form->fields('', '', 10)->size(0, 12)->showLabel(false)->with(
                    function () use ($form, $isEdit) {
                        $form->defaultDisplayerSize(12, 12);
                        $form->number('bucket_start', '大于等于≥')->help('累计下级会员购买的桶装水数量');
                        $form->number('bucket_end', '小于等于≤')->help('累计下级会员购买的桶装水数量，-1表示不限制');
                    }
                );
            }
        );

        $form->tab('方式2/采购架子爬坡升级')->with(
            function () use ($form, $isEdit) {
                $form->fields('', '', 10)->size(0, 12)->showLabel(false)->with(
                    function () use ($form, $isEdit) {
                        $form->defaultDisplayerSize(12, 12);
                        $form->number('shelf_start', '大于等于≥')->help('累计下级会员购买水架数量');
                        $form->number('shelf_end', '小于等于≤')->help('累计下级会员购买水架数量，-1表示不限制');
                    }
                );
            }
        );
//        $form->match('','指定模式：产品管理->分成配置:按配置的产品和配置的等级来升级');

        $form->tab('方式3/购买推客升级礼包');
        $form->match('', '产品管理->分成配置:按配置的产品和配置的等级来升级');

        $form->tab('附加条件')->with(
            function () use ($form, $isEdit) {
                $form->fields('', '', 10)->size(0, 12)->showLabel(false)->with(
                    function () use ($form, $isEdit) {
                        $form->defaultDisplayerSize(12, 12);
                        $form->number('direct_level', '直推等级')->default(0)->help('直推等级，默认不限制');
                        $form->number('direct_num', '直推数量')->default(0)->help('直推等级数量，默认不限制');
                        $form->number('amount', '消费金额')->help('消费满多少金额，默认0');
                    }
                );
            }
        );
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'name',
            'level',
            'description',
            'score',
            'bucket_start',
            'bucket_end',
            'shelf_start',
            'shelf_end',
            'direct_level',
            'direct_num',
            'amount',
        ], 'post');

        $result = $this->validate($data, [
            'name|名称' => 'require',
            'level|等级' => 'require|number'
        ]);

        if (true !== $result) {
            $this->error($result);
        }

        if ($id) {
            $data['update_time'] = date_now();
        } else {
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }
}
